//
//  UIKitExampleStackView02Controller.m
//  MasonryExample
//
//  Created by 山刘 on 2020/6/12.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "UIKitExampleStackView02Controller.h"

@interface UIKitExampleStackView02Controller ()

@end

@implementation UIKitExampleStackView02Controller{
    UIStackView *stackView;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"StackView02";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self test7];
}

-(void)test7{
    
    UIView *subView1 = [LayoutUtils createView];
    UIView *subView2 = [LayoutUtils createView];
    UIView *subView3 = [LayoutUtils createView];
    
    subView1.tag = 0;
    subView2.tag = 1;
    subView3.tag = 2;
    
    subView1.backgroundColor = UIColor.redColor;
    subView2.backgroundColor = UIColor.greenColor;
    subView3.backgroundColor = UIColor.blueColor;
    
    [subView1 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(40, 100))).priorityLow();
    }];
    [subView2 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(100, 80))).priorityLow();
    }];
    [subView3 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(70, 120))).priorityLow();
    }];
    
    stackView = [[UIStackView alloc] initWithArrangedSubviews:@[subView1,subView2,subView3]];
    [self.view addSubview:stackView];
    // 不设置宽度，让它宽度自适应
    [stackView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(@100);
        make.right.equalTo(self.view);
        make.height.equalTo(@200);
    }];
    
    stackView.distribution = UIStackViewDistributionFill;
    stackView.alignment = UIStackViewAlignmentCenter;
    stackView.spacing = 5;
}

-(void)test6{
    
    NSMutableArray<UIView*> *imageViewArray = [NSMutableArray array];
    for (int i = 0; i < 5; i ++) {
        UIView *subView = [LayoutUtils createView];
        [subView makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(@(CGSizeMake(50, 50)));
        }];
        subView.layer.cornerRadius = 25;
        subView.layer.masksToBounds = YES;
        subView.clipsToBounds = true;
        [imageViewArray addObject:subView];
    }
    
    UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:imageViewArray];
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.alignment = UIStackViewAlignmentCenter;
    stackView.spacing = -15;
    [self.view addSubview:stackView];
    
    [stackView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(@100);
        //make.right.equalTo(self.view);
        make.height.equalTo(@50);
    }];
}

-(void)test5{
    
    UIView *subView1 = [LayoutUtils createView];
    UIView *subView2 = [LayoutUtils createView];
    UIView *subView3 = [LayoutUtils createView];
    
    subView1.tag = 0;
    subView2.tag = 1;
    subView3.tag = 2;
    
    subView1.backgroundColor = UIColor.redColor;
    subView2.backgroundColor = UIColor.greenColor;
    subView3.backgroundColor = UIColor.blueColor;
    
    [subView1 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(40, 100))).priorityLow();
    }];
    [subView2 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(100, 80))).priorityLow();
    }];
    [subView3 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(70, 120))).priorityLow();
    }];
    
    stackView = [[UIStackView alloc] initWithArrangedSubviews:@[subView1,subView2,subView3]];
    [self.view addSubview:stackView];
    // 不设置宽度，让它宽度自适应
    [stackView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(@100);
        make.right.equalTo(self.view);
        make.height.equalTo(@200);
    }];
    
    stackView.distribution = UIStackViewDistributionEqualCentering;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.spacing = 10;
}

-(void)test4{
    
    UIView *subView1 = [LayoutUtils createView];
    UIView *subView2 = [LayoutUtils createView];
    UIView *subView3 = [LayoutUtils createView];
    
    subView1.tag = 0;
    subView2.tag = 1;
    subView3.tag = 2;
    
    subView1.backgroundColor = UIColor.redColor;
    subView2.backgroundColor = UIColor.greenColor;
    subView3.backgroundColor = UIColor.blueColor;
    
    [subView1 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(40, 100))).priorityLow();
    }];
    [subView2 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(100, 80))).priorityLow();
    }];
    [subView3 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(70, 120))).priorityLow();
    }];
    
    stackView = [[UIStackView alloc] initWithArrangedSubviews:@[subView1,subView2,subView3]];
    [self.view addSubview:stackView];
    // 不设置宽度，让它宽度自适应
    [stackView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(@100);
        make.right.lessThanOrEqualTo(self.view);
        make.height.equalTo(@200);
    }];
    
    stackView.distribution = UIStackViewDistributionEqualSpacing;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.spacing = 0;
}

-(void)test3{
    
    UIView *subView1 = [LayoutUtils createView];
    UIView *subView2 = [LayoutUtils createView];
    UIView *subView3 = [LayoutUtils createView];
    
    subView1.tag = 0;
    subView2.tag = 1;
    subView3.tag = 2;
    
    subView1.backgroundColor = UIColor.redColor;
    subView2.backgroundColor = UIColor.greenColor;
    subView3.backgroundColor = UIColor.blueColor;
    
    [subView1 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(40, 100))).priorityLow();
    }];
    [subView2 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(100, 80))).priorityLow();
    }];
    [subView3 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(70, 120))).priorityLow();
    }];
    
    stackView = [[UIStackView alloc] initWithArrangedSubviews:@[subView1,subView2,subView3]];
    [self.view addSubview:stackView];
    // 不设置宽度，让它宽度自适应
    [stackView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(@100);
        make.right.lessThanOrEqualTo(self.view);
        make.height.equalTo(@200);
    }];
    
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.spacing = 0;
}

-(void)test2{
    
    UIView *subView1 = [LayoutUtils createView];
    UIView *subView2 = [LayoutUtils createView];
    UIView *subView3 = [LayoutUtils createView];
    
    subView1.tag = 0;
    subView2.tag = 1;
    subView3.tag = 2;
    
    subView1.backgroundColor = UIColor.redColor;
    subView2.backgroundColor = UIColor.greenColor;
    subView3.backgroundColor = UIColor.blueColor;
    
    [subView1 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(1, 200))).priorityLow();
    }];
    [subView2 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(2, 200))).priorityLow();
    }];
    [subView3 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(3, 200))).priorityLow();
    }];
    
    stackView = [[UIStackView alloc] initWithArrangedSubviews:@[subView1,subView2,subView3]];
    [self.view addSubview:stackView];
    // 不设置宽度，让它宽度自适应
    [stackView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(@100);
        make.right.equalTo(self.view);
        make.height.equalTo(@200);
    }];
    
    stackView.distribution = UIStackViewDistributionFillProportionally;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.spacing = 5;
    
}

-(void)test1{
    
    UIView *subView1 = [LayoutUtils createView];
    UIView *subView2 = [LayoutUtils createView];
    UIView *subView3 = [LayoutUtils createView];
    
    subView1.tag = 0;
    subView2.tag = 1;
    subView3.tag = 2;
    
    subView1.backgroundColor = UIColor.redColor;
    subView2.backgroundColor = UIColor.greenColor;
    subView3.backgroundColor = UIColor.blueColor;
    
    [subView1 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(130, 100)));
    }];
    [subView2 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(130, 80)));
    }];
    [subView3 makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(130, 120))).priorityLow();
    }];
    
    stackView = [[UIStackView alloc] initWithArrangedSubviews:@[subView1,subView2,subView3]];
    [self.view addSubview:stackView];
    // 不设置宽度，让它宽度自适应
    [stackView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(kPadding);
        make.top.equalTo(@100);
        make.right.lessThanOrEqualTo(kPadding);
        make.height.equalTo(200);
    }];
    
    stackView.alignment = UIStackViewAlignmentBottom;
    stackView.distribution = UIStackViewDistributionEqualSpacing;
    stackView.spacing = 30;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    for (UIView *object in stackView.arrangedSubviews) {
        if (object.tag == 1) {
            [object setHidden:YES]; // [object removeFromSuperview];
            [UIView animateWithDuration:1.0 animations:^{
                [self->stackView layoutIfNeeded];
            }];
        }
    }
}
@end
